package cn.edu.qlnu.xky.test.controller;

import cn.edu.qlnu.xky.test.model.User;
import cn.edu.qlnu.xky.test.service.ReaderService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author : wuweihong
 * @desc : 实现读者的增删改查及重置密码功能
 * @date : 2025-06-20
 */


public class ReaderController extends HttpServlet {

	private static final String ACTION_ADD = "add";
	private static final String ACTION_SEARCH = "search";
	private static final String ACTION_EDIT = "edit";
	private static final String ACTION_DO_EDIT = "doEdit";
	private static final String ACTION_DELETE = "delete";
	private static final String ACTION_RESET_PWD = "resetPwd";

	private static final String URL_READER_ADD_JSP = "/reader/add.jsp";
	private static final String URL_READER_SEARCH_JSP = "/reader/search.jsp";
	private static final String URL_READER_EDIT_JSP = "/reader/edit.jsp";

	private static final String MSG_READER_ADD_SUCCESS = "读者添加成功";
	private static final String MSG_READER_ADD_FAIL = "读者添加失败";
	private static final String MSG_READER_ADD_EXIST = "读者添加失败";
	private static final String MSG_READER_ADD_ERROR = "读者添加操作出现异常，请咨询管理员";

	private static final String MSG_READER_EDIT_SUCCESS = "修改成功";
	private static final String MSG_READER_EDIT_FAIL = "修改失败";
	private static final String MSG_READER_EDIT_EXIST = "修改失败, userName已存在";

	private static final String MSG_READER_DELETE_SUCCESS = "删除成功";
	private static final String MSG_READER_DELETE_FAIL = "删除失败";
	private static final String MSG_READER_DELETE_ERROR = "读者删除操作出现异常，请咨询管理员";

	private static final String MSG_READER_RESET_PWD_SUCCESS = "重置密码成功";
	private static final String MSG_READER_RESET_PWD_FAIL = "重置密码失败";
	private static final String MSG_READER_RESET_PWD_ERROR = "读者重置密码操作出现异常，请咨询管理员";


	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReaderController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("能否在后台看到我？我是ReaderController的代码");
		request.setCharacterEncoding("utf-8");

		// 操作的标志，null是添加前的页面，add为添加，search为查询
		String action = request.getParameter("flag");
		String url = null;
		System.out.println("flag=" + action);
		if (action == null) {
			url = URL_READER_ADD_JSP;
		} else {
			ReaderService service = new ReaderService();
			if (ACTION_ADD.equals(action)) {
				String userDisplayName = request.getParameter("userDisplayName");
				String userName = request.getParameter("userName");
				String temp = request.getParameter("temp");
				// 应该封装这些信息，然后传递到后台类方法，存到数据库中
				List<User> readers = service.searchEqualName(userName);
				String msg = "";
				if (Objects.nonNull(readers) && readers.size() > 0) {
					msg = MSG_READER_ADD_EXIST;
				}else {
					User reader = new User();
					reader.setUserName(userName);
					reader.setUserDisplayName(userDisplayName);
					reader.setTemp(temp);

					int ret = service.addReader(reader);

					if (ret >= 1) {
						msg = MSG_READER_ADD_SUCCESS;
					} else if (ret == 0) {
						msg = MSG_READER_ADD_FAIL;
					} else if (ret == -1) {
						msg = MSG_READER_ADD_ERROR;
					}
				}
				request.setAttribute("msg", msg);
				url = URL_READER_SEARCH_JSP;
			} else if (ACTION_SEARCH.equals(action)) {
				String nameForSearch = request.getParameter("nameForSearch");
				if(nameForSearch == null) {
					nameForSearch="";
				}
				String pageNo = request.getParameter("pageNo");
				// 此处为查询模块
				request.setAttribute("page", service.searchByNamePage(nameForSearch,Integer.parseInt(pageNo)));
				url = URL_READER_SEARCH_JSP;
			} else if (action.equals("edit")) {
				// 修改前的查询操作
				String id = request.getParameter("id");
				User reader = service.searchById(Integer.parseInt(id));
				request.setAttribute("reader", reader);
				url = URL_READER_EDIT_JSP;
			}else if(action.equals("doEdit")) {
				//真正的修改操作
				request.setCharacterEncoding("UTF-8");
				String id = request.getParameter("id");
				String userName = request.getParameter("userName");
				String userDisplayName = request.getParameter("userDisplayName");
				String temp = request.getParameter("temp");
				List<User> readers = service.searchEqualName(userName);
				String msg = "";
				boolean userNameDuplicate = false;
				if (Objects.nonNull(readers) && readers.size() > 0) {
					for (User reader : readers) {
						if (reader.getId()!=Integer.parseInt(id)) {
							msg = "修改失败, userName已存在";
							userNameDuplicate = true;
						}
					}
				}
				if (!userNameDuplicate){
					User reader = new User();
					reader.setId(Integer.parseInt(id));
					reader.setUserDisplayName(userDisplayName);
					reader.setUserName(userName);
					reader.setTemp(temp);


					int ret = service.editReader(reader);
					if (ret == 0) {
						msg = "修改失败";
					} else {
						msg = "修改成功";
					}
				}
				//TODO 后期要删除
				request.setAttribute("msg", msg);
				url = "/reader/search.jsp";
			}else if(action.equals("delete")) {
				//进行删除操作
				String id = request.getParameter("id");
				System.out.println("id="+id);
				int ret = service.deleteReader(Integer.parseInt(id));
				String msg = "";
				if(ret == 0) {
					msg ="删除失败";
				}else if(ret >=1){
					msg ="删除成功";
				}else if(ret == -1) {
					msg = "读者删除操作出现异常，请咨询管理员";
				}
				request.setAttribute("msg", msg);
				url = "/reader/search.jsp";
			}else if(action.equals("resetPwd")) {
				//进行重置密码操作
				String id = request.getParameter("id");
				System.out.println("id="+id);
				int ret = service.resetPwdReader(Integer.parseInt(id));
				String msg = "";
				if(ret == 0) {
					msg ="重置密码失败";
				}else if(ret >=1){
					msg ="重置密码成功";
				}else if(ret == -1) {
					msg = "读者重置密码操作出现异常，请咨询管理员";
				}
				request.setAttribute("msg", msg);
				url = "/reader/search.jsp";
			}
		}
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String url) throws ServletException, IOException {
		request.getRequestDispatcher(url).forward(request, response);
	}

}
